package com.lizhi.rpc.handler;

import com.lizhi.common.yun.entity.RpcClientPOJO;
import com.lizhi.common.yun.entity.RpcServicePOJO;
import com.lizhi.common.yun.enums.DataUpStateEnum;
import com.lizhi.common.yun.enums.HeartBeartEnum;
import com.lizhi.yun.db.stand.dao.StandAssessInfoDao;
import com.lizhi.yun.db.stand.pojo.StandAssessInfoPOJO;
import com.lizhi.common.yun.entity.UpDataPOJO;
import com.lizhi.utils.DateUtils;
import com.lizhi.utils.StringUtils;
import com.lizhi.utils.json.SamJsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Author: lizhi
 * @Description:
 * @Date: create in 2020/8/24 15:08
 */

@Slf4j
@Service
public class RecvAssessHandler {

    @Resource
    private StandAssessInfoDao standAssessInfoDao;


    /**
     * 接收同步的评估信息数据
     * @param json
     * @return
     */
    public String recvAssess(String json){
        UpDataPOJO upDataPOJO = SamJsonUtil.toObject(json, UpDataPOJO.class);
        RpcClientPOJO rpcClientPOJO = upDataPOJO.getRpcClientPOJO();

        RpcServicePOJO rpcServicePOJO = new RpcServicePOJO();
        rpcServicePOJO.setSystemType(rpcClientPOJO.getSystemType());
        rpcServicePOJO.setOrganId(rpcClientPOJO.getOrganId());
        rpcServicePOJO.setBkOrganId(rpcClientPOJO.getBkOrganId());
        if (StringUtils.isEmpty(rpcClientPOJO)){
            rpcServicePOJO.setErrorCode(HeartBeartEnum.HEART_BEART_000001.getEnValue());
            rpcServicePOJO.setMessage(HeartBeartEnum.HEART_BEART_000001.getEnName());
            return SamJsonUtil.toJson(rpcServicePOJO);
        }

        /*评估信息*/
        StandAssessInfoPOJO standAssessInfoPOJO = SamJsonUtil.toObject(upDataPOJO.getData(), StandAssessInfoPOJO.class);
        if (StringUtils.isEmpty(standAssessInfoPOJO)){
            rpcServicePOJO.setErrorCode(HeartBeartEnum.HEART_BEART_000002.getEnValue());
            rpcServicePOJO.setMessage(HeartBeartEnum.HEART_BEART_000002.getEnName());
            return SamJsonUtil.toJson(rpcServicePOJO);
        }
        /*将医院编号和设备编号放进来，以判断是哪个设备传输的*/
        standAssessInfoPOJO.setHospitalId(rpcClientPOJO.getOrganId());
        standAssessInfoPOJO.setHospitalSystemId(rpcClientPOJO.getBkOrganId());
        /*数据上传的时间*/
        standAssessInfoPOJO.setAssUpState(DataUpStateEnum.DATA_UP_STATE_01.getEnValue());
        standAssessInfoPOJO.setAssUpTm(DateUtils.getNowDate());
        log.info("上传的数据信息是standAssessInfoPOJO:{}", standAssessInfoPOJO);

        StandAssessInfoPOJO assessInfoPOJO = standAssessInfoDao.selectById(standAssessInfoPOJO.getAssId(), standAssessInfoPOJO.getHospitalId(), standAssessInfoPOJO.getHospitalSystemId());
        if (StringUtils.isEmpty(assessInfoPOJO)){
            standAssessInfoPOJO.setLid(StringUtils.newId());
            standAssessInfoDao.insertRecord(standAssessInfoPOJO);
        } else {
            standAssessInfoDao.updateByIdSelective(standAssessInfoPOJO);
        }

        rpcServicePOJO.setErrorCode(HeartBeartEnum.HEART_BEART_000000.getEnValue());
        rpcServicePOJO.setMessage(HeartBeartEnum.HEART_BEART_000000.getEnName());
        return SamJsonUtil.toJson(rpcServicePOJO);
    }
}
